package com.weijian.controller;

import com.weijian.common.result.ApiResponse;
import com.weijian.dal.entity.Task;
import com.weijian.service.TaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 任务管理接口
 */
@Tag(name = "任务管理", description = "任务增删改查接口")
@RestController
@RequestMapping("/api/tasks")
public class TaskController {

    @Autowired
    private TaskService taskService;

    /**
     * 查询所有任务
     *
     * @return 任务列表
     */
    @Operation(summary = "查询所有任务")
    @GetMapping
    public ApiResponse<List<Task>> getAllTasks() {
        List<Task> tasks = taskService.list();
        return ApiResponse.success(tasks);
    }
    
    /**
     * 根据dataId查询任务
     *
     * @param dataId 数据ID
     * @return 任务列表
     */
    @Operation(summary = "根据dataId查询任务")
    @GetMapping("/by-data")
    public ApiResponse<List<Task>> getTasksByDataId(
            @Parameter(description = "数据ID") @RequestParam Integer dataId) {
        List<Task> tasks = taskService.getTasksByDataId(dataId);
        return ApiResponse.success(tasks);
    }

    /**
     * 根据ID查询任务
     *
     * @param id 任务ID
     * @return 任务信息
     */
    @Operation(summary = "根据ID查询任务")
    @GetMapping("/{id}")
    public ApiResponse<Task> getTaskById(
            @Parameter(description = "任务ID") @PathVariable Integer id) {
        Task task = taskService.getById(id);
        if (task == null) {
            return ApiResponse.notFound("任务不存在");
        }
        return ApiResponse.success(task);
    }
}